<?php
/**
 * 消费管理
 * @author tangbl
 *
 */
class SellAction extends CommonAction {
 
    /**
     * 初始化
     */
    public function _initialize(){
    
    	parent::_initialize();
    	//parent::checkTimestamp();
    }
    
    /**
     * 查询客户计次消费项目列表
     * @access public
     * @param  String $dataid
     */
    public function numberUser(){
    	
    	$map['company'] = $this->_req("company", "intval",0);
    	$map['dataid']  = $this->_get("dataid",  "trim",  "");
    	$map['isdel']   = 2;//未使用
    	$model = M("OrderNumber");
    	$list  = $model->where($map)->select();
    	//echo $model->getLastSql();
    	$this->success($list);
    }
    
    /**
     * 查询计次消费项目详情
     * @access public
     * @param  int    $id
     */
    public function numberDetail(){
    	
    	$map['company'] = $this->_req("company", "intval",0);
    	$map['id']      = $this->_req("id"     , "intval",0);
    	//$map['isdel']   = 2;//未使用
    	 
    	$model = M("OrderNumber");
    	$rows  = $model->where($map)->find();
    	$this->success($rows);
    }
    
    /**
     * 查询计次消费项目详情
     * @access public
     * @param  int      $numid
     * @param  varchar  $vipno
     */
    public function numberLog(){
    	
    	$map['v.numid']   = $this->_req("numid",   "intval",0);
    	$map['v.dataid']  = $this->_req("dataid",   "trim",  "");
    	//$map['isdel']   = 2;//未使用
    
    	$model = D("SellNumLogView");
    	$rows  = $model->where($map)->select();
    	$this->success($rows);
    }
    
    
    /**
     * 使用计次消费项目
     * @access public
     * @param  array  $map     应用参数(company,outletid,addid,addno)
     * @param  int    $id      计次id
     * @param  String $content 备注
     */
    public function numberExpense(){
    	
    	$num  = $this->_req("num", "intval",1);
    	
    	$map['company'] = $this->_req("company", "intval",0);
    	$map['id']      = $this->_req("id"     , "intval",0);
    	$map['isdel']   = 2;//未使用
    
    	$model = M("OrderNumber");
    	$rows  = $model->where($map)->find();
    	$remain= $rows["remain"];
    	
    	if( $rows['id'] && $num<=$remain ){
    		
    		$model->where("id=".$rows['id'])->setDec("remain", $num);
    		$data = parent::getParams();
    		$data['content'] = $this->_req("content", "trim", "");
    		$data['dataid']  = $this->_req("dataid",  "trim", "");
    		$data['tableid'] = $this->_req("tableid", "trim", "");
    		$data["numid"]   = $rows['id'];
    		$data["num"]     = $num;
    		$data["typeid"]  = 3;
    		$data["addtime"] = time();
    		
    		$id = M("OrderExpense")->add($data);
    		$this->success($id);
    	}else{
    		$this->error("剩余次数不足");
    	}
    }
    
    /**
     * 添加计时项目
     * @access public
     * @param  array  $map     应用参数(company,outletid,addid,addno)
     * @param  Int    $pid     商品id
     * @param  String $tableid 用户模型[VipcardUser] required
     * @param  String $dataid  用户编号[10086] required
     * @param  int    $rent    押金
     * @param  int    $num     限时数量[0不限时]
     * @param  String $content 备注
     */
    public function timeAdd(){
    	
    	$param = array(
    		"before" => "order_timeAdd_before",
    		"after"  => "order_timeAdd_after",
    	);
    	parent::do_add("Order",$param);
    }
    
    
    /**
     * 已有计时项目开始
     * @access public
     * @access public
     * @param  array  $map  应用参数(company,outletid,addid,addno)
     * @param  int    $id
     * @param  int    $rent 押金
     */
    public function timeBegin(){
    	
    	$map['company'] = $this->_req("company", "intval",0);
    	$map['id']      = $this->_req("id"     , "intval",0);
    	$map['isdel']   = 2;//未使用
    	 
    	$model = M("OrderTime");
    	$rows  = $model->where($map)->find();
    	
    	if($rows['id']){
    		
    		$hour  = 60*60;
    		$num   = intval($rows['amount'])*$hour;
    		$now   = time();
    		if($num){
    			$data['plan'] = $now+$num;
    		}
    		$data['isdel'] = 3;
    		$data['begin'] = time();
    		
    		$id = $model->where("id=".$rows['id'])->save($data);
    		$this->success($id);
    	}else{
    		$this->error();
    	}
    }
    
    /**
     * 计时项目结束
     * @access public
     * @access public
     * @param  array  $map  应用参数(company,outletid,addid,addno)
     * @param  int    $id   计时项目编号 required
     */
    public function timeEnd(){
    	 
    	$map['company'] = $this->_req("company", "intval",0);
    	$map['id']      = $this->_req("id"     , "intval",0);
    	$map['isdel']   = 3;//使用中
    
    	$model = M("OrderTime");
    	$rows  = $model->where($map)->find();
    	 
    	if($rows['id']){
    		
    		$moud  = D("Order");
    		$order = $rows['orderno'];
    		$begin = $rows['begin'];
    		$now   = time();
			$num   = $now-$begin;
			$price = $moud->calcProductTime($rows['pid'], $num);
			
			//计时结束需要改变订单价格
			$moud->calcOrderTime($rows, $price);
			
			$data  = array("end"=>$now, "isdel"=>1);
			$id    = $model->where("id=".$rows['id'])->save($data);
			
			if($id){
				$this->success($rows['orderno']);
			}else{
				$this->error($id);
			}
    	}else{
    		$this->error();
    	}
    }
    
    
    /**
     * 查询当前进行计时消费项目
     * @access public
     */
    public function timeNowItem(){
		
    	$map['isdel']   = 3;//使用中
    	$map['end']     = 0;
    	$map['company'] = $this->_req("company", "intval",0);
    	
    	$model = M("OrderTime");
    	$list  = $model->where($map)->select();
    	$this->success($list);
    }
    
    /**
     * 查询客户计时消费项目
     * @access public
     * @param  String $dataid
     */
    public function timeUser(){
    	 
    	$map['company'] = $this->_req("company", "intval",0);
    	$map['dataid']  = $this->_req("dataid" , "trim"  ,'');
    	$map['isdel']   = 2;//未使用
    	 
    	$model = M("OrderTime");
    	$list  = $model->where($map)->select();
    	$this->success($list);
    }
    
    
}